/*
 *@Type CmdClient.java
 * @Desc
 * @Author urmsone urmsone@163.com
 * @date 2024/6/13 13:58
 * @version
 */
package client;

import java.util.Scanner;

public class CmdClient{

    private Client client;

    public CmdClient(Client client) {
        this.client = client;
    }

    public void start() {
        Scanner scan = new Scanner(System.in);
        while (true) {
            // 将输入的字符串转换为数组
            System.out.print(">>> ");
            String inp = scan.nextLine();
            String[] parts = inp.split("\\s+");

            if (parts.length == 0) continue;

            String action = parts[0];

            // 判断输入的命令
            if (action.equals("exit")) {
                break;

            } else if (action.equals("set") && parts.length == 3) {
                String key = parts[1];
                String value = parts[2];
                client.set(key, value);

            } else if (action.equals("get") && parts.length == 2) {
                String key = parts[1];
                String value = client.get(key);
                System.out.println("Value: " + value);

            } else if (action.equals("rm") && parts.length == 2) {
                String key = parts[1];
                client.rm(key);
            } else {
                System.out.println("指令格式错误!");
            }
        }

        scan.close();

    }

}
